﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;

using Cards.GUI;
using Cards.Util;
using System.Windows.Forms;

namespace Cards.Objects
{
    /// <summary>
    /// Base class for multi network playing
    /// </summary>
    public class MultiGame
    {
        public String testTxt;

        /// <summary>
        /// Server port
        /// </summary>
        protected String m_port;

        /// <summary>
        /// Server IP
        /// </summary>
        protected String m_IP;

        /// <summary>
        /// The netowrk connection GUI
        /// </summary>
        protected FrmCreateMultiGame m_frmParentMultiGame;

        public String PORT
        {
            get { return m_port; }
            set { m_port = value; }
        }
        public String IP
        {
            get { return m_IP; }
            set { m_IP = value; }
        }
        public FrmCreateMultiGame PARENTFORM
        {
            get { return m_frmParentMultiGame; }
            set { m_frmParentMultiGame = value; }
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="_parentForm"></param>
        public MultiGame(FrmCreateMultiGame _parentForm)
        {
            m_frmParentMultiGame = _parentForm;
        }

        /// <summary>
        /// Utility function to get IP address of the current machine
        /// </summary>
        /// <returns></returns>
        public String GetIP()
        {
            String strHostName = Dns.GetHostName();
            String IPStr = "";

            try
            {
                // Find host by name
                IPHostEntry iphostentry = Dns.GetHostByName(strHostName);

                // Grab the first IP addresses
                foreach (IPAddress ipaddress in iphostentry.AddressList)
                {
                    IPStr = ipaddress.ToString();
                    return IPStr;
                }
            }
            catch (SystemException se)
            {
                MessageBox.Show(se.Message);
            }

            return IPStr;
        }
        
        /// <summary>
        /// Virtual function to start listening/connect
        /// </summary>
        /// <returns></returns>
        public virtual Boolean Start()
        {
            return true;
        }

        /// <summary>
        /// Virtual function to Stop listening/connect
        /// </summary>
        public virtual void Stop()
        {
        }

        /// <summary>
        /// Virtual function to send message
        /// </summary>
        /// <param name="_str"></param>
        public virtual void SendMessage(String _str)
        {
        }
                
        /// <summary>
        /// Virtual function to start the network game
        /// </summary>
        /// <param name="_message"></param>
        public virtual void BeginGame(String _message)
        {
        }

        /// <summary>
        /// Virtual function to continue the network game
        /// </summary>
        /// <param name="_message"></param>
        public virtual void ContinueGame(String _message)
        {
        }

        /// <summary>
        /// Virtual function to send a specific command around the network
        /// </summary>
        /// <param name="_command"></param>
        /// <param name="_message"></param>
        /// <param name="_id"></param>
        public virtual void SendCommand(String _command, String _message, int _id = -1)
        {
        }

        /// <summary>
        /// Virtual function to send a MOVE command
        /// </summary>
        /// <param name="_message"></param>
        /// <param name="_id"></param>
        public virtual void SendAMove(String _message, int _id = -1)
        {
        }

        /// <summary>
        /// Virtual function to send a SKIP command
        /// </summary>
        /// <param name="_message"></param>
        /// <param name="_id"></param>
        public virtual void SendASkip(String _message, int _id = -1)
        {
        }
    }

    /// <summary>
    /// Class to contain network information of a client
    /// </summary>
    public class SocketPacket
    {
        public Socket m_currentSocket;
        public byte[] m_dataBuffer = new byte[Constants.MAX_DATA_TRANSFER];
        public int m_id;
        public String m_name;
    }

    /// <summary>
    /// Class to contain information of a network player
    /// </summary>
    public class PlayerStruct
    {
        public int m_id;
        public String m_name;
        public PLAYER_TYPE m_playerType;
    }
}
